package com.ysy.logistics.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysy.logistics.entities.oam.OperationLog;
import com.ysy.logistics.entities.system.SysNotice;
import com.ysy.logistics.entities.system.UserNotice;
import com.ysy.logistics.entities.system.dto.UserNoticeDto;
import com.ysy.logistics.mapper.system.SysNoticeMapper;
import com.ysy.logistics.mapper.system.UserNoticeMapper;
import com.ysy.logistics.myenum.NoticeType;
import com.ysy.logistics.service.system.UserNoticeService;
import com.ysy.logistics.util.PageInfo;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 姚斯羽
 * @date 2025/4/18 11:35
 * @description:
 */
@Service
public class UserNoticeServiceImpl extends ServiceImpl<UserNoticeMapper, UserNotice> implements UserNoticeService {
    @Resource
    private UserNoticeMapper userNoticeMapper;
    @Resource
    private SysNoticeMapper sysNoticeMapper;
    @Override
    public List<SysNotice> getByDeviceNoticeUserId(String userId) {
        if (StringUtils.isBlank(userId)){
            return null;
        }
        //根据消息类型和用户Id读取消息
        return sysNoticeMapper.getNoticeByUserIdAndType(userId, NoticeType.DEVICE_NOTICE.getCode());
    }

    @Override
    public List<SysNotice> getByStoreNoticeUserId(String userId) {
        if (StringUtils.isBlank(userId)){
            return null;
        }
        return sysNoticeMapper.getNoticeByUserIdAndType(userId, NoticeType.NOTICE_TYPE_STOCK.getCode());
    }

    @Override
    public Page<UserNoticeDto> listByPage(PageInfo<UserNoticeDto> pageInfo) {
        LocalDate beginTime = pageInfo.getBeginTime();
        LocalDate endTime = pageInfo.getEndTime();
        if (beginTime != null && endTime != null){
            LocalDate temp = beginTime;
            endTime = beginTime.isAfter(endTime)?beginTime:endTime;
            beginTime = temp;
        }
        LocalDateTime localBeginDateTime = null;
        LocalDateTime localEndDateTime = null;
        if (beginTime!=null){
             localBeginDateTime = beginTime.atStartOfDay();
        }
        if (endTime!=null){
            //+1天
            endTime.plusDays(1);
            localEndDateTime = endTime.atStartOfDay();

        }
        return userNoticeMapper.listByPage(pageInfo.getPage(), pageInfo.getCriteria(), localBeginDateTime, localEndDateTime);
    }

    @Override
    public void remove(PageInfo<UserNoticeDto> pageInfo) {
        LocalDate beginTime = pageInfo.getBeginTime();
        LocalDate endTime = pageInfo.getEndTime();
        if (beginTime != null && endTime != null){
            LocalDate temp = beginTime;
            endTime = beginTime.isAfter(endTime)?beginTime:endTime;
            beginTime = temp;
        }
        LocalDateTime localBeginDateTime = null;
        LocalDateTime localEndDateTime = null;
        if (beginTime!=null){
            localBeginDateTime = beginTime.atStartOfDay();
        }
        if (endTime!=null){
            //+1天
            endTime.plusDays(1);
            localEndDateTime = endTime.atStartOfDay();

        }
         userNoticeMapper.remove(pageInfo.getCriteria(), localBeginDateTime, localEndDateTime);
    }
}
